package com.wangchao.administrator.zenmechi.ui.searchactivity;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/18.
 */
public class SearchActivity extends BaseActivity implements
        SearchActivityContract.View, View.OnClickListener {


    @BindView(R.id.image_back)
    ImageView imageBack;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_category)
    TextView tvCategory;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_clear_history)
    TextView tvClearHistory;
    @BindView(R.id.lv_search)
    PullToRefreshListView lvSearch;
    @BindView(R.id.tv_none)
    TextView tvNone;
    private SearchActivityPresenter presenter;
    private ListView lv;
    private PopupWindow mPopupWindow;
    private TextView tv_product, tv_goods, tv_merchant;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        initViews();
        lv = lvSearch.getRefreshableView();
        lvSearch.setMode(PullToRefreshBase.Mode.BOTH);
        presenter = new SearchActivityPresenter(this);
        presenter.searchHistory(this);
        initPopWindow();
    }

    private void initPopWindow() {
        View popupView = LayoutInflater.from(SearchActivity.this).
                inflate(R.layout.popup_select_search, null);
        mPopupWindow = initWindowSettings(popupView);
        tvCategory.setOnClickListener(this);
        tv_goods = (TextView) popupView.findViewById(R.id.tv_goods);
        tv_product = (TextView) popupView.findViewById(R.id.tv_product);
        tv_merchant = (TextView) popupView.findViewById(R.id.tv_merchant);
        tv_goods.setOnClickListener(this);
        tv_product.setOnClickListener(this);
        tv_merchant.setOnClickListener(this);
    }

    private void setWwindowAlpha() {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = 0.7f;
        getWindow().setAttributes(params);
    }

    private void closePopupWindow(PopupWindow popupWindow) {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.alpha = 1f;
            getWindow().setAttributes(params);
        }
    }

    private PopupWindow initWindowSettings(View view) {
        PopupWindow popupWindow = null;
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        popupWindow.getContentView().setFocusableInTouchMode(true);
        popupWindow.getContentView().setFocusable(true);


        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.alpha = 1f;
                getWindow().setAttributes(params);
            }
        });
        return popupWindow;
    }

    @Override
    public void initViews() {
        imageBack.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        tvClearHistory.setOnClickListener(this);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                presenter.search(SearchActivity.this);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                SearchActivity.this.finish();
                break;
            case R.id.tv_cancel:
                SearchActivity.this.finish();
                break;
            case R.id.tv_category:
                setWwindowAlpha();
                mPopupWindow.showAtLocation(tvCategory, Gravity.CENTER, 0, 0);
                break;
            case R.id.tv_goods:
                closePopupWindow(mPopupWindow);
                tvCategory.setText("菜谱");
                break;
            case R.id.tv_product:
                closePopupWindow(mPopupWindow);
                tvCategory.setText("产品");
                break;
            case R.id.tv_merchant:
                closePopupWindow(mPopupWindow);
                tvCategory.setText("商铺");
                break;
            case R.id.tv_clear_history:
                presenter.clearHistory(SearchActivity.this);
                break;
        }
    }

    @Override
    public void showNone() {
        tvNone.setVisibility(View.VISIBLE);
        lvSearch.setVisibility(View.GONE);
    }

    @Override
    public void hideNone() {
        tvNone.setVisibility(View.GONE);
        lvSearch.setVisibility(View.VISIBLE);
    }

    @Override
    public String getSearchType() {
        return tvCategory.getText().toString().trim();
    }

    @Override
    public String getSearchName() {
        return etSearch.getText().toString().trim();
    }

    @Override
    public void initSearchCaipu(Adapter adapter) {
        lvSearch.setAdapter((ListAdapter) adapter);
    }
}
